<template>
  <div>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品分类</el-breadcrumb-item>
      </el-breadcrumb>
    <el-card class="box-card">
        <el-button type="primary" @click="addCate">添加分类</el-button>
        <tree-grid :data="goodsCate" :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        border
        :show-row-hover="false">
          <template slot="isok" slot-scope="scope">
            <i class="el-icon-success" v-if="scope.row.cat_deleted='false'" style="color: green"></i>
            <i class="el-icon-error" v-else-if="scope.row.cat_deleted='true'" style="color: red"></i>
          </template>
          <template slot="version" slot-scope="scope">
            <el-tag v-if="scope.row.cat_level === 0">等级一</el-tag>
            <el-tag type="warning" v-else-if="scope.row.cat_level=== 1">等级二</el-tag>
            <el-tag type="success" v-else>等级三</el-tag>
          </template>
          <template slot="edit" slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="EditCate(scope.row)">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="delRow(scope.row.cat_id)">删除</el-button>
          </template>
        </tree-grid>
        <!-- 表格分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[5, 8, 10, 12]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
<el-dialog
  title="添加分类"
  :visible.sync="addCatedialogVisible"
  width="30%">
  <el-form ref="addCateformRef" :model="addCateform" label-width="120px"
   :rules="addCateRules">
    <el-form-item label="分类名称" prop="cat_name">
      <el-input v-model="addCateform.cat_name"></el-input>
    </el-form-item>
    <el-form-item label="父级分类">
      <el-cascader placeholder="请选择" v-model="Catevalue" :options="parentCate" :props="parentCateProps" @change="CateChange">
      </el-cascader>
    </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="addCatedialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="AddgoodsCate">确 定</el-button>
  </span>
</el-dialog>
<el-dialog
  title="修改分类信息"
  :visible.sync="EditCatedialogVisible"
  width="30%">
    <el-form :model="editCateList" ref="editCateRef" :rules="addCateRules">
      <el-form-item label="分类名称" prop="cat_name" label-width="100px">
        <el-input type="text" v-model="editCateList.cat_name"></el-input>
      </el-form-item>
    </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="EditCatedialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="PutCate">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryData: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      goodsCate: [],
      //  商品的总数
      total: 0,
      columns: [{
        label: '分类名称',
        prop: 'cat_name'
      },
      {
        label: '是否有效',
        //  当前列定义为模板列 开启插槽
        type: 'template',
        //  具名插槽名称（也是作用域插槽）
        template: 'isok'
      },
      {
        label: '排序',
        // prop: 'cat_level',
        //  开启插槽
        type: 'template',
        template: 'version'
      },
      {
        label: '操作',
        type: 'template',
        template: 'edit'
      }
      ],
      addCatedialogVisible: false,
      addCateform: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      addCateRules: {
        cat_name: [{ required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }]
      },
      //  级联选择器双向绑定的数据
      Catevalue: [],
      parentCate: [],
      parentCateProps: {
        expandTrigger: 'hover',
        label: 'cat_name',
        value: 'cat_id',
        checkStrictly: true
      },
      EditCatedialogVisible: false,
      editCateList: {}
    }
  },

  created () {
    this.getGoodsList()
    // console.log(this.Catevalue)
  },

  methods: {
    async getGoodsList () {
      const { data: res } = await this.axios.get('categories', { params: this.queryData })
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.goodsCate = res.data.result
      this.total = res.data.total
    },
    //  每页条数发生变化
    handleSizeChange (val) {
      this.queryData.pagesize = val
      this.getGoodsList()
    },
    //  页码发生变化
    handleCurrentChange (val) {
      this.queryData.pagenum = val
      this.getGoodsList()
    },
    async addCate () {
      this.addCatedialogVisible = true
      this.getParentCate()
    },
    //  获取父级分类
    async getParentCate () {
      const { data: res } = await this.axios.get('categories', {
        params: {
          type: 2
        }
      })
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.parentCate = res.data
      // console.log(this.Catevalue)
    },
    CateChange () {
      // console.log(this.Catevalue)
      //  如果 Catevalue数组中的length大于0 证选中的父级分类
      //  小于0 就没有选中任何父级分类
      if (this.Catevalue.length > 0) {
        this.addCateform.cat_pid = this.Catevalue[this.Catevalue.length - 1]
        this.addCateform.cat_level = this.Catevalue.length
      } else {
        this.addCateform.cat_pid = 0
        this.addCateform.cat_level = 0
      }
    },
    //  添加分类
    AddgoodsCate () {
      this.$refs.addCateformRef.validate(async flag => {
        const { data: res } = await this.axios.post('categories', {
          cat_pid: this.addCateform.cat_pid,
          cat_name: this.addCateform.cat_name,
          cat_level: this.addCateform.cat_level
        })
        if (res.meta.status !== 201) return this.$message.error('添加失败')
        this.addCatedialogVisible = false
        this.getGoodsList()
        this.$message.success('添加成功')
      })
    },
    //  删除功能
    delRow (id) {
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.axios.delete('categories/' + id)
        if (res.meta.status !== 200) return this.$message.error('删除失败')
        this.getGoodsList()
        this.$message.success('删除成功!')
      }).catch(() => {
        this.$message('已取消删除')
      })
    },
    //  编辑分类
    EditCate (row) {
      this.EditCatedialogVisible = true
      this.editCateList = JSON.parse(JSON.stringify(row))
    },
    async PutCate () {
      const id = this.editCateList.cat_id
      const CatName = this.editCateList.cat_name
      const { data: res } = await this.axios.put('categories/' + id, {
        cat_name: CatName
      })
      if (res.meta.status !== 200) return this.$message.error('编辑失败')
      this.getGoodsList()
      this.$refs.editCateRef.resetFields()
      this.EditCatedialogVisible = false
      this.$message.success('编辑成功')
    }

  }
}
</script>

<style scoped lang='less'>
</style>
